<template>
    <div>
        <ly-dialog v-model="dialogVisible" :title="loadingTitle" width="560px" :before-close="handleClose">
            <el-form :inline="false" :model="formData" :rules="rules" ref="rulesForm" label-position="right" label-width="auto">
                <el-form-item label="编号：" prop="sn">
                    <el-input v-model="formData.sn"></el-input>
                </el-form-item>
                <el-form-item label="建筑名称" prop="house_name">
                    <el-input v-model="formData.house_name"></el-input>
                </el-form-item>
                <el-form-item label="建筑类型" prop="house_type">
                    <el-input v-model="formData.house_type"></el-input>
                </el-form-item>
                <el-form-item label="建筑地址" prop="address">
                    <el-input v-model="formData.address"></el-input>
                </el-form-item>
                <el-form-item label="潜力等级" prop="level">
                    <el-input v-model="formData.level"></el-input>
                </el-form-item>
                <el-form-item label="户数" prop="house_man">
                    <el-input v-model="formData.house_man"></el-input>
                </el-form-item>
                <el-form-item label="经度" prop="house_lat">
                    <el-input v-model="formData.house_lat"></el-input>
                </el-form-item>
                <el-form-item label="维度" prop="house_lng">
                    <el-input v-model="formData.house_lng"></el-input>
                </el-form-item>
                <tmap-map mapKey="kM5zkEP9wuGI7vRApG9mhNY4Yef3BWLl'" :center="center" :zoom="zoom" :control="control" :events="events">
                    <tmap-multi-label :id="id" :styles="styles" :geometries="geometries" />
                </tmap-map>
            </el-form>
            <template #footer>
                <el-button @click="handleClose" :loading="loadingSave">取消</el-button>
                <el-button type="primary" @click="submitData" :loading="loadingSave" :disabled="loadingTitle=='详情'">确定</el-button>
            </template>
        </ly-dialog>
    </div>
</template>

<script>
    import {mallGoodsbrandAdd,mallGoodsbrandEdit} from "@/api/api";
    import LyDialog from "../../../components/dialog/dialog";
    export default {
        emits: ['refreshData'],
        name: "addModuleGoodsType",
        components: {LyDialog},
        data() {
            return {
                dialogVisible:false,
                loadingSave:false,
                loadingTitle:'',
                formData:{
                    name:"",
                    sort:0,
                    prefix:"",
                },
                center:{ lat: 39.984120, lng: 116.307484 },
                zoom:10,
                geometries:[
                    {
                        id: 'label', // 点图形数据的标志信息
                        styleId: 'label', // 样式id
                        position: { lat: 39.984120, lng: 116.307484 }, // 标注点位置
                        content: '腾讯北京总部', // 标注文本
                        properties: { title: 'label'},
                    },
                ],
                styles:{
                    label: {
                        color: '#3777FF', // 颜色属性
                        size: 20, // 文字大小属性
                        offset: { x: 0, y: 0 }, // 文字偏移属性单位为像素
                        angle: 0, // 文字旋转属性
                        alignment: 'center', // 文字水平对齐属性
                        verticalAlignment: 'middle', // 文字垂直对齐属性
                    },
                },
                control: {
                    scale: {},
                    zoom: {
                            position: 'bottomRight',
                        },
                },
                events: {
                    "click": this.print,
                },
                rules:{
                    name: [
                        {required: true, message: '请输入橱窗名称',trigger: 'blur'}
                    ],
                }
            }
        },
        methods:{
            print(evt){
                var lat = evt.latLng.getLat().toFixed(6);
                var lng = evt.latLng.getLng().toFixed(6);
                console.log("您点击的的坐标是："+ lat + "," + lng);
                this.formData.house_lat=lat
                this.formData.house_lng=lng

            },
            handleClose() {
                this.dialogVisible=false
                this.loadingSave=false
                this.formData = {
                    name:"",
                    sort:0,
                    prefix:"",
                }
                this.$emit('refreshData')
            },
            addModuleFn(item,flag) {
                this.loadingTitle=flag
                this.dialogVisible=true
                if(item){
                    this.formData=item
                    this.center.lat=item.house_lat
                    this.center.lng=item.house_lng
                    this.geometries[0].position.lat=item.house_lat
                    this.geometries[0].position.lng=item.house_lng
                    this.geometries[0].content = item.house_name
                }
            },
            submitData() {
                this.$refs['rulesForm'].validate(obj=>{
                    if(obj) {
                        this.loadingSave=true
                        let param = {
                            ...this.formData
                        }
                        if(this.formData.id){
                            mallGoodsbrandEdit(param).then(res=>{
                                this.loadingSave=false
                                if(res.code ==2000) {
                                    this.$message.success(res.msg)
                                    this.handleClose()
                                    this.$emit('refreshData')
                                } else {
                                    this.$message.warning(res.msg)
                                }
                            })
                        }else{
                            mallGoodsbrandAdd(param).then(res=>{
                                this.loadingSave=false
                                if(res.code ==2000) {
                                    this.$message.success(res.msg)
                                    this.handleClose()
                                    this.$emit('refreshData')
                                } else {
                                    this.$message.warning(res.msg)
                                }
                            })
                        }
                    }
                })
            }
        }
    }
</script>
<style scoped>
    .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 128px;
      height: 128px;
      line-height: 128px;
      text-align: center;
    }
    .avatar {
      width: 128px;
      height: 128px;
      display: block;
    }
</style>

